Skip to content

Fix LongCat n-gram history for right-padded batches - #2103

Open
zsun6 wants to merge 1 commit into
ml-explore:mainfrom
zsun6:fix/longcat-ngram-batched-history
Open

zsun6 wants to merge 1 commit into
ml-explore:mainfrom
zsun6:fix/longcat-ngram-batched-history

Conversation

@zsun6

@zsun6 zsun6 commented Nov 24, 2026

Copy link
Copy Markdown

NgramEmbedding saved the last n - 1 physical tokens of each row as its history. In a right-padded batch those are padding for the shorter rows, so a short prompt batched with a longer one gives different output than when it runs alone (first token 105 alone, 121 batched). This uses the cache lengths to save each row's last real tokens.

With small random LongCat weights, 18 of 28 batched trajectories matched their single runs on main, 28 of 28 with this change. Added test_longcat_ngram_padded_history, which fails on main and passes here.

  • ☑️ I understand it is strictly prohibited to use AI to write PR description
  • AI usage disclosure: AI helped write the code and run the experiments; I reviewed all of it in depth.

In a right-padded batch, the n-gram cache kept the last n - 1 physical
tokens of each row, so a short prompt next to a longer one saved padding
as its history and its next tokens changed. Save each row's last real
tokens using the cache lengths instead.

AI helped write the code and run the experiments; I reviewed all of it in depth.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant